﻿//-----------------------------------------------------------------------
// <copyright file="ControlBase.cs" company="deixei.com">
//     Copyright (c) deixei.com. All rights reserved.
// </copyright>
// <author>Marcio Parente</author>
//---------------------------------------------------------------------
namespace Deixei.Web
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Web.Mvc;
    using System.Web.Routing;

    /// <summary>
    /// Control Base
    /// </summary>
    public abstract class ControlBase
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ControlBase" /> class.
        /// </summary>
        /// <param name="tagName">Name of the tag.</param>
        protected ControlBase(string tagName)
        {
            this.TagName = tagName;
        }

        /// <summary>
        /// Gets or sets the control id.
        /// </summary>
        /// <value>
        /// The control id.
        /// </value>
        public string ControlId
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the core class.
        /// </summary>
        /// <value>
        /// The core class.
        /// </value>
        public string CoreClass
        {
            get;
            protected set;
        }

        /// <summary>
        /// Gets or sets the CSS class.
        /// </summary>
        /// <value>
        /// The CSS class.
        /// </value>
        public string CssClass
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the enhancement CSS class.
        /// </summary>
        /// <value>
        /// The enhancement CSS class.
        /// </value>
        public string EnhancementCssClass
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the HTML attributes.
        /// </summary>
        /// <value>
        /// The HTML attributes.
        /// </value>
        public object HtmlAttributes
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the name of the tag.
        /// </summary>
        /// <value>
        /// The name of the tag.
        /// </value>
        public string TagName
        {
            get;
            set;
        }

        /// <summary>
        /// To the HTML.
        /// </summary>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <returns>Mvc Html String</returns>
        public virtual MvcHtmlString ToHtml(HtmlHelper htmlHelper)
        {
            return this.ToHtml(htmlHelper, null);
        }

        /// <summary>
        /// To the HTML.
        /// </summary>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="htmlAttributes">The HTML attributes.</param>
        /// <returns>Mvc Html String</returns>
        public virtual MvcHtmlString ToHtml(HtmlHelper htmlHelper, IDictionary<string, object> htmlAttributes)
        {
            TagBuilder tag = new TagBuilder(this.TagName);
            if (this.HtmlAttributes != null)
            {
                tag.MergeAttributes<string, object>(new RouteValueDictionary(this.HtmlAttributes));
            }

            tag.MergeAttributes<string, object>(htmlAttributes);
            if (!string.IsNullOrEmpty(this.ControlId))
            {
                tag.GenerateId(this.ControlId);
            }

            if (!string.IsNullOrEmpty(this.CoreClass))
            {
                tag.AddCssClass(this.CoreClass);
            }

            if (!string.IsNullOrEmpty(this.EnhancementCssClass))
            {
                tag.AddCssClass(this.EnhancementCssClass);
            }

            if (!string.IsNullOrEmpty(this.CssClass))
            {
                tag.AddCssClass(this.CssClass);
            }

            StringBuilder contents = new StringBuilder();
            this.WriteHtmlContents(htmlHelper, tag, contents);
            tag.InnerHtml = contents.ToString();
            return MvcHtmlString.Create(tag.ToString());
        }

        /// <summary>
        /// Writes the HTML contents.
        /// </summary>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="tag">The tag.</param>
        /// <param name="contents">The contents.</param>
        protected virtual void WriteHtmlContents(HtmlHelper htmlHelper, TagBuilder tag, StringBuilder contents)
        {
        }
    }
}
